package com.wzdigit.wms.wms.service.core;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wzdigit.wms.basic.domain.dtos.transfer.UpdateGdnDtlQtyDto;
import com.wzdigit.wms.basic.domain.transfer.DocPreAloc;
import com.wzdigit.wms.basic.domain.transfer.GdnDtl;
import com.wzdigit.wms.basic.domain.transfer.GdnHeader;
import com.wzdigit.wms.basic.mapper.core.transfer.DocPreAlocMapper;
import com.wzdigit.wms.basic.mapper.core.transfer.GdnDtlMapper;
import com.wzdigit.wms.basic.mapper.core.transfer.GdnHeaderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 发货单公共服务类
 *
 * @author yangdongjie
 * @GdnService.java
 * @2021年2月24日下午3:25:03
 */
@Service
public class GdnCoreService {

    @Autowired
    private GdnDtlMapper gdnDtlMapper;

    @Autowired
    private GdnHeaderMapper gdnHeaderMapper;

    @Autowired
    private DocPreAlocMapper docPreAlocMapper;

    /**
     * 更新发货单明细数量-服务
     *
     * @param dto
     */
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void updateGdnDtlQty(UpdateGdnDtlQtyDto dto) {
        gdnDtlMapper.updateGdnDtlQty(dto);
    }

    /**
     * 根据发货单明细主键集合查询发货单明细信息
     *
     * @param gdnDtlIdList
     * @return
     */
    public List<GdnDtl> getGdnDtlListByPKList(List<Long> gdnDtlIdList) {
        //创建类型的条件构造器
        QueryWrapper<GdnDtl> wrapper = new QueryWrapper<>();
        wrapper.in("GDN_DTL_ID", gdnDtlIdList);

        List<GdnDtl> gdnDtlList = gdnDtlMapper.selectList(wrapper);
        return gdnDtlList;
    }

    /**
     * 根据 明细主键查询明细
     *
     * @param gdnDtlId
     * @return
     */
    public GdnDtl getGdnDtlByGdnDtlId(Long gdnDtlId) {

        GdnDtl gdnDtl = gdnDtlMapper.selectById(gdnDtlId);
        return gdnDtl;
    }

    /**
     * 根据发货单明细查询发货单预配表
     *
     * @param gdnDtlId
     * @return
     */
    public List<DocPreAloc> getGdnPreAlocByGdnId(Integer gdnDtlId, Integer status) {

        //创建类型的条件构造器
        QueryWrapper<DocPreAloc> wrapper = new QueryWrapper<>();
        wrapper.eq("DOC_DTL_ID", gdnDtlId);
        wrapper.eq("STATUS", status);
        List<DocPreAloc> gdnPreAlocList = docPreAlocMapper.selectList(wrapper);

        return gdnPreAlocList;
    }

    /**
     * 根据主键更新状态
     *
     * @param gdnDtlId
     * @param code
     * @param loginName
     * @param now
     * @return
     */
    public Integer updateGdnDtlStatusByPk(Long gdnDtlId, int code, String loginName, Date now) {

        GdnDtl gdnDtl = new GdnDtl();
        gdnDtl.setGdnDtlId(gdnDtlId).setStatus(code);

        gdnDtl.setModifyTime(now);
        gdnDtl.setModifyUser(loginName);


        int row = gdnDtlMapper.updateById(gdnDtl);

        return row;
    }


    /**
     * 根据发货明细ID查询
     *
     * @param gdnHeaderId
     * @return
     */
    public GdnHeader getHeadByHeadId(Long gdnHeaderId) {

        GdnHeader gdnHeader = gdnHeaderMapper.selectById(gdnHeaderId);
        return gdnHeader;
    }

    /**
     * 根据单头获取发货单明细
     *
     * @param gdnHeaderId
     * @return
     */
    public List<GdnDtl> getGdnDtlByHeadId(Long gdnHeaderId) {

        //创建类型的条件构造器
        QueryWrapper<GdnDtl> wrapper = new QueryWrapper<>();
        wrapper.eq("GDN_HEADER_ID", gdnHeaderId);
        List<GdnDtl> gdnDtlList = gdnDtlMapper.selectList(wrapper);
        return gdnDtlList;
    }

    /**
     * 根据明细主键更新数量
     *
     * @param docDtlId
     * @param pickQty
     * @return
     */
    public Integer updateGdnDtlPickQty(Long docDtlId, BigDecimal pickQty, String loginName) {

        GdnDtl gdnDtl = new GdnDtl();
        gdnDtl.setGdnDtlId(docDtlId);
        gdnDtl.setPickQty(pickQty);
        gdnDtl.setModifyTime(new Date());
        gdnDtl.setModifyUser(loginName);

        int row = gdnDtlMapper.updateById(gdnDtl);

        return row;
    }

    /**
     * @param headIdList
     * @return
     */
    public List<GdnHeader> getHeadByHeadIdList(List<Long> headIdList) {

        //创建类型的条件构造器
        QueryWrapper<GdnHeader> wrapper = new QueryWrapper<>();
        wrapper.in("GDN_HEADER_ID", headIdList);

        List<GdnHeader> gdnHeaderList = gdnHeaderMapper.selectList(wrapper);
        return gdnHeaderList;
    }

    /**
     * @param gdnIdList
     * @param commonFlag
     * @return
     */
    public Integer updateDtlStatusByDtlIdList(List<Integer> gdnIdList, Integer commonFlag) {

        //创建类型的条件构造器
        QueryWrapper<GdnDtl> wrapper = new QueryWrapper<>();
        wrapper.in("GDN_DTL_ID", gdnIdList);

        GdnDtl gdnDtl = new GdnDtl();
        gdnDtl.setPickTaskFlag(commonFlag);

        int rowUpdate = gdnDtlMapper.update(gdnDtl, wrapper);
        return rowUpdate;
    }

    /**
     * 根据明细ID更新【生成拣货任务】标识
     *
     * @param docDtlId
     * @param picktaskFlag
     * @return
     */
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public Integer updateDtlStatusByDtlId(Long docDtlId, Integer picktaskFlag, String name) {

        GdnDtl gdnDtl = new GdnDtl();
        gdnDtl.setPickTaskFlag(picktaskFlag);
        gdnDtl.setGdnDtlId(docDtlId);
        gdnDtl.setModifyUser(name);
        gdnDtl.setModifyTime(new Date());

        int rowUpdate = gdnDtlMapper.updateById(gdnDtl);
        return rowUpdate;
    }

    /**
     * 根据主键更新主档状态
     *
     * @param gdnHeaderId
     * @param code
     * @param loginName
     * @param nowDate
     * @return
     */
    public Integer updateGdnHeaderStatusByPk(Integer gdnHeaderId, int code, String loginName, Date nowDate) {

        GdnHeader gdnHeader = new GdnHeader();
        gdnHeader.setStatus(code);
        gdnHeader.setModifyTime(nowDate);
        gdnHeader.setModifyUser(loginName);

        //创建类型的条件构造器
        QueryWrapper<GdnHeader> wrapper = new QueryWrapper<>();
        wrapper.ne("STATUS", code);
        wrapper.eq("GDN_HEADER_ID", gdnHeaderId);


        int updateRow = gdnHeaderMapper.update(gdnHeader, wrapper);

        return updateRow;
    }

    /**
     * 查询符合下发条件的发货单明细
     * @param commonTrue
     * @return
     */
    public List<GdnDtl> selectAllowIssueList(int commonTrue) {

        //创建类型的条件构造器
        QueryWrapper<GdnDtl> wrapper = new QueryWrapper<>();
        wrapper.ne("PICK_TASK_FLAG", commonTrue).or().isNull("PICK_TASK_FLAG");

        List<GdnDtl> gdnDtlList = gdnDtlMapper.selectList(wrapper);
        return gdnDtlList;
    }

    /**
     * 查询符合拣货条件的发货单明细
     * @param gdnTypeList
     * @param commonFlag
     * @return
     */
    public List<GdnDtl> selectAllowCreatePickTask(List<String> gdnTypeList, Integer commonFlag) {

        List<GdnDtl> gdnDtlList = gdnDtlMapper.selectAllowCreatePickTask(gdnTypeList, commonFlag);
        return gdnDtlList;
    }

    public GdnHeader selectBySourceShipNotice(String postbackSourceNo) {

        QueryWrapper<GdnHeader> wrapper = new QueryWrapper<>();
        wrapper.eq("SOURCE_SHIP_NOTICE", postbackSourceNo);

        GdnHeader gdnHeader = gdnHeaderMapper.selectOne(wrapper);
        return gdnHeader;

    }

    public GdnHeader getGdnHeaderByGdnNo(String postbackNo) {
        QueryWrapper<GdnHeader> wrapper = new QueryWrapper<>();
        wrapper.eq("GDN_NO",postbackNo );

        GdnHeader gdnHeader = gdnHeaderMapper.selectOne(wrapper);

        return gdnHeader;
    }

    public Integer updateGdnDtlPickQty2(Long docDtlId, BigDecimal qty, String loginName) {

        GdnDtl gdnDtl = new GdnDtl();
        gdnDtl.setGdnDtlId(docDtlId);
        gdnDtl.setPickQty(qty);
        gdnDtl.setModifyTime(new Date());
        gdnDtl.setModifyUser(loginName);

        int row = gdnDtlMapper.updateGdnDtlPickQty2(gdnDtl);

        return row;
    }
}
